using UnityEngine;
using System.Collections;

public class CharMovement : MonoBehaviour {
	
	public float dmovSpeed;
	private float movSpeed;
	
	public Ray ray;
	public RaycastHit hit;
	
	public GameObject Char;
	public GameObject bulletSpawner;
    public GameObject pauseMenu;

	public Transform barHealth;
	public Transform bullet;
    public Transform fadeOut;
	public float interval = 0.35f;
	
	public AudioClip fireSound;
	
	private int hp = 100;
	public int damage = 20;

	static public bool playing = false;
    public bool showBool = false;

	private int kills;
	public TextMesh textKill;
    private float timePassed;
	
	public string dieScene;

	void Start () 
	{
        PlayerPrefs.DeleteAll();
        StartCoroutine("startGame");

        Char.animation["Walk"].speed = 2;
        Char.animation.Play("Walk");
	}

    IEnumerator startGame()
    {
        yield return new WaitForSeconds(0.7f);
        playing = true;
    }
	
	void Update () 
	{
        textKill.text = "Enemies Killed: " + kills.ToString();

        timePassed += Time.deltaTime;

		if (playing == true)
		{
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			transform.Translate (Vector3.forward * Input.GetAxis("Vertical") * movSpeed);
			transform.Translate (Vector3.right * Input.GetAxis("Horizontal") * movSpeed);

            if(showBool == false)
            {
                if(Input.GetAxis("Vertical") != 0 && Input.GetAxis("Horizontal") != 0)
                {
                    movSpeed = dmovSpeed / 1.2f;
                }
                else
                {
                    movSpeed = dmovSpeed;
                }

                if(Input.GetMouseButtonDown(0))
                {
                    InvokeRepeating("shoot", 0.01f, interval);
                    audio.PlayOneShot(fireSound);
                }
                if(Input.GetMouseButtonUp(0))
                {
                    CancelInvoke("shoot");
                }

                if(Physics.Raycast(ray, out hit))
                {
                    Vector3 hitPos = new Vector3(hit.point.x, Char.transform.position.y, hit.point.z);
                    Char.transform.LookAt(hitPos);
                }

            }
			
			if (Input.GetAxis("Vertical")!= 0 || Input.GetAxis("Horizontal") != 0)
			{
                Char.animation.Play("Walk");
			}   
			
			if (Input.GetAxis("Vertical")== 0 && Input.GetAxis("Horizontal") == 0)
			{
                Char.animation.Stop("Walk");
			}
			
	        
            if(Input.GetKeyDown(KeyCode.Escape))
            {
                Time.timeScale = 1 - Time.timeScale;
                showBool = !showBool;
                movSpeed = 0;

                ShowPauseGUI(showBool);
            }
		}
    }
	
	public void AddedKill()
	{
        kills++;
        PlayerPrefs.SetInt("Kills", kills);
	}
	
	public void removeHP (int damage)
	{
		this.hp -= damage;

		float posX = hp;
		barHealth.localScale = new Vector3(posX/100,barHealth.localScale.y,barHealth.localScale.z);

        if (hp <= 0)
        {
            gameOver();
        }
	}
	
	public void shoot ()
	{
		Instantiate(bullet, new Vector3(bulletSpawner.transform.position.x, bulletSpawner.transform.position.y, bulletSpawner.transform.position.z), Char.transform.rotation);
	}
	
	void gameOver ()
	{
		hp = 0;
        barHealth.localScale = new Vector3(hp / 100, barHealth.localScale.y, barHealth.localScale.z);

        Char.animation.Play("Dead");
		CancelInvoke("shoot");
		playing = false;
        StartCoroutine("CallDieScene");

        PlayerPrefs.SetFloat("TimeAlive", timePassed);

        Instantiate(fadeOut, fadeOut.transform.position, Quaternion.identity);
	}
		
	IEnumerator CallDieScene()
	{
		yield return new WaitForSeconds(2);
		Application.LoadLevel(dieScene);
	}

    public void ShowPauseGUI(bool state)
    {
        pauseMenu.SetActive(state);
    }
}

